package com.xuecheng.learning.service;

import com.xuecheng.framework.domain.course.response.TeachplanMediaPub;
import com.xuecheng.framework.domain.learning.response.GetMediaResult;
import com.xuecheng.framework.domain.learning.response.LearningCode;
import com.xuecheng.framework.exception.ExceptionCast;
import com.xuecheng.framework.model.response.CommonCode;
import com.xuecheng.learning.client.CourseSearchClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class CourseLearningService {

    @Autowired
    private CourseSearchClient courseSearchClient;
    /**
     * 获取课程计划对应的视频资源
     * @param courseId 课程id, 用于校验学生权限
     * @param teachplanId 课程计划id, 通过es查找对应的视频
     * @return 视频播放地址
     */
    public GetMediaResult getMedia(String courseId, String teachplanId) {
        // 1. 通过courseId, 校验学生权限

        //2. 远程调用es搜索服务获取视频地址
        TeachplanMediaPub teachplanMediaPub = courseSearchClient.getMedia(teachplanId);
            //获取失败,则抛出异常
        String mediaUrl = teachplanMediaPub.getMediaUrl();
        if (teachplanMediaPub == null || StringUtils.isEmpty(mediaUrl)) {
            ExceptionCast.cast(LearningCode.LEARNING_GETMEDIA_ERROR);
        }
        // 3. 响应结果
        return new GetMediaResult(CommonCode.SUCCESS, mediaUrl);
    }
}
